package com.linkwedo.saas.member;

import org.apache.commons.mail.EmailException;
import org.hibernate.exception.GenericJDBCException;

import com.linkwedo.saas.directory.LDAPService;
import com.linkwedo.saas.domain.member.RegisterCompanyVerify;
import com.linkwedo.saas.domain.member.User;
import com.linkwedo.saas.email.EmailService;
import com.linkwedo.saas.support.BaseAction;

/**
 * user name should not be saas, because that is the database name...
 * @author rabbit
 *
 */
public class RegisterCompanyAction extends BaseAction {

	private static final long serialVersionUID = 3170917054103860557L;

	/**
	 * 注册第一步，发送邮件到用户邮箱
	 * @return
	 * @throws Exception 
	 */
	public String sendEmail() throws Exception {
		final String ERROR = "register_1_error";
		final String SUCCESS = "register_1_success";
		if (LDAPService.isEmailExist(getVerify().getEmail())) {
			addActionError("Email has already registered");
			return ERROR;
		}
		if (LDAPService.isCompanyExist(getVerify().getCompany())) {
			addActionError("Company has already registered");
			return ERROR;
		}
		try {
		String verifyCode = getRegisterService().persistVerify(getVerify().getEmail(), getVerify().getCompany());
		EmailService.sendRegisterCompany(getVerify().getEmail(), getVerify().getCompany(), verifyCode);
		} catch (EmailException exception) {
			addActionError("Email service is not available");
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 注册第二步，用户从邮箱中打开了链接
	 * 
	 */
	public String toRegister() {
//		Map<String, Object> map = ActionContext.getContext().getParameters();
//		String email =  ((String[])map.get("email"))[0];
//		String verifyCode =  ((String[]) map.get("verifyCode"))[0];
//		
//		if (!getRegisterService().verify((String)email, (String)verifyCode)) {
//			addActionError("Invalid email");
//			return "register_2_error";
//		}
		return "toRegister_3";
	}
	
	
	/**
	 * 注册第3步
	 * 注册成功的条件：1.校验码通过  2. 公司不存在  3.email没注册过 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		final String SUCCESS = "register_3_success";
		final String ERROR = "register_3_error";
		try {
			if (!getRegisterService().verify(getUser().getEmail(), getVerifyCode())) {
				addActionError("Invalid email and verify code");
				return ERROR;
			}
			if (LDAPService.isCompanyExist(getUser().getCompanyName())) {
				addActionError("Register failed because company had already registered.");
				return ERROR;
			}
			if (LDAPService.isEmailExist(getUser().getEmail())) {
				addActionError("Register failed because email had already registered.");
				return ERROR;
			}
			getRegisterService().registerCompany(getUser());
			
		} catch(GenericJDBCException e) {
			//如果用户尝试以数据库已有的数据名注册
			addActionError("You can't register because malformed email, please change to another email");
			return ERROR;
		}
//		getSession().put(Constants.USER, user);
//		getSession().put(Constants.JNDIURLKEY, ConnectionStrategy.generateJNDIKey(user));
		return SUCCESS;
	}
	
	public String toRegisterCompany() {
		return "register_1";
	}
	
	private User user = new User();
	private RegisterCompanyVerify verify = new RegisterCompanyVerify();
	private RegisterCompanyService registerService;

	private String verifyCode;
	
	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public User getUser() {
		return user;
	}
	
	public RegisterCompanyVerify getVerify() {
		return verify;
	}

	public void setVerify(RegisterCompanyVerify verify) {
		this.verify = verify;
	}

	public RegisterCompanyService getRegisterService() {
		return registerService;
	}

	public void setRegisterService(RegisterCompanyService registerService) {
		this.registerService = registerService;
	}
}
